package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SUserOperateRecordMapper;
import com.ruoyi.system.domain.SUserOperateRecord;
import com.ruoyi.system.service.ISUserOperateRecordService;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-05
 */
@Service
public class SUserOperateRecordServiceImpl extends ServiceImpl<SUserOperateRecordMapper, SUserOperateRecord> implements ISUserOperateRecordService
{
    @Autowired
    private SUserOperateRecordMapper userOperateRecordMapper;
    @Autowired
    private ISysUserService sysUserService;

    @Override
    public List<SUserOperateRecord> getList(SUserOperateRecord sUserOperateRecord) {

        SysUser user = SecurityUtils.getLoginUser().getUser();


        QueryWrapper<SUserOperateRecord> queryWrapper = new QueryWrapper<SUserOperateRecord>();
        queryWrapper.like(StringUtils.isNotBlank(sUserOperateRecord.getNickName()),"u.nick_name", sUserOperateRecord.getNickName());
        queryWrapper.like(StringUtils.isNotBlank(sUserOperateRecord.getDeviceName()),"sd.device_name", sUserOperateRecord.getDeviceName());
        if(StringUtils.isNotNull(sUserOperateRecord.getSubmitTime())) {
            Date submitTime = sUserOperateRecord.getSubmitTime();
            String s = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, submitTime);
            queryWrapper.between( "uor.submit_time", s + " 00:00:00",s + " 23:59:59");
        }
        // 班组长只能查看自己和自己组员的提交记录
        if(user.getUserType().equals(UserType.LEADER.getCode())){
            List<Long> userIdList = sysUserService.list(new LambdaQueryWrapper<SysUser>()
                    .eq(SysUser::getLeaderId, user.getUserId())
                    .or().eq(SysUser::getUserId, user.getUserId())).stream().map(SysUser::getUserId).collect(Collectors.toList());
            queryWrapper.in("uor.user_id", userIdList);
        }

        queryWrapper.orderByDesc("uor.create_time");
        return userOperateRecordMapper.getList(queryWrapper);
    }
}
